using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class AudioManager : MonoBehaviour {
    
    [Header("事件监听")]
    public PlayAudioEventSO FXEvent;        // 音效播放事件
    public PlayAudioEventSO BGMEvent;       // BGM播放事件
    
    [Header("组件")]
    public AudioSource BGMSource;           // BGM
    public AudioSource FXSource;            // 音效
    public Slider volumeSlider;             // 滑动控制器组件，用于控制音量 

    private void Start() {
        // 音量控制器的值等于当前的音量
        volumeSlider.value = AudioListener.volume;
        // 音量控制器滑动改变的事件监听
        volumeSlider.onValueChanged.AddListener(OnChangeVolume);
    }

    private void OnEnable() {
        FXEvent.OnEventRaised += OnFXEvent;
        BGMEvent.OnEventRaised += OnBGMEvent;
    }

    private void OnDisable() {
        FXEvent.OnEventRaised -= OnFXEvent;
        BGMEvent.OnEventRaised -= OnBGMEvent;
    }

    private void OnFXEvent(AudioClip audioClip) {
        FXSource.clip = audioClip;
        FXSource.Play();
    }

    private void OnBGMEvent(AudioClip audioClip) {
        BGMSource.clip = audioClip;
        BGMSource.Play();
    }

    public void OnChangeVolume(float volume) {
        // 将音量控制器的值赋值给AudioListenner的volume，以改变音量
        AudioListener.volume = volume;
        Debug.Log(AudioListener.volume);
    }

}